/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import model.Locatie;

/**
 *
 * @author misja
 */
public class LocatieDialoog extends javax.swing.JFrame {

    /**
     * Creates new form LocatieDialoog
     */
    private char actieCode;
    private String huisnummer;
    private String postcode;
    private Locatie clsLocatie;

    public LocatieDialoog(char argActieCode, String argHuisnummer, String argPostcode) {
        initComponents();

        this.actieCode = argActieCode;
        this.postcode = argPostcode;
        this.huisnummer = argHuisnummer;

        if (this.postcode.isEmpty() || this.huisnummer.isEmpty()) {
            Locatie locatie = new Locatie();
            this.clsLocatie = locatie;
        } else {
            Locatie locatie = new Locatie(this.huisnummer, this.postcode);
            this.clsLocatie = locatie;
        }

        txtNaam.setText(clsLocatie.getNaam());
        txtPlaats.setText(clsLocatie.getPlaats());
        txtStraat.setText(clsLocatie.getStraat());
        txtHuisnummer.setText(clsLocatie.getHuisnummer());
        txtPostcode.setText(clsLocatie.getPostcode());
        txtDeelnemers.setText(Integer.toString(clsLocatie.getDeelnemers()));

        switch (this.actieCode) {
            case 't':
                btnOke.setText("Toevoegen");
                setTitle("LOCATIE TOEVOEGEN");
                break;
            case 'b':
                btnOke.setText("Bijwerken");
                setTitle("LOCATIE BIJWERKEN");
                break;
            case 'v':
                btnOke.setText("Verwijderen");
                setTitle("LOCATIE VERWIJDEREN");
                txtNaam.setEditable(false);
                txtPlaats.setEditable(false);
                txtStraat.setEditable(false);
                txtHuisnummer.setEditable(false);
                txtPostcode.setEditable(false);
                txtDeelnemers.setEditable(false);
                break;
        }
    }

    LocatieDialoog() {       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNaam = new javax.swing.JLabel();
        lblPlaats = new javax.swing.JLabel();
        lblStraat = new javax.swing.JLabel();
        lblHuisnummer = new javax.swing.JLabel();
        lblPostcode = new javax.swing.JLabel();
        lblDeelnemers = new javax.swing.JLabel();
        txtNaam = new javax.swing.JTextField();
        txtPlaats = new javax.swing.JTextField();
        txtStraat = new javax.swing.JTextField();
        txtHuisnummer = new javax.swing.JTextField();
        txtPostcode = new javax.swing.JTextField();
        txtDeelnemers = new javax.swing.JTextField();
        btnOke = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("LOCATIE TOEVOEGEN");
        setName("Locatie"); // NOI18N

        lblNaam.setText("naam ");

        lblPlaats.setText("plaats ");

        lblStraat.setText("straat ");

        lblHuisnummer.setText("huisnummer ");

        lblPostcode.setText("postcode ");

        lblDeelnemers.setText("deelnemers ");

        txtNaam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNaamActionPerformed(evt);
            }
        });

        txtHuisnummer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtHuisnummerActionPerformed(evt);
            }
        });

        txtDeelnemers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDeelnemersActionPerformed(evt);
            }
        });

        btnOke.setText("Toevoegen");
        btnOke.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkeActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNaam, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblPlaats, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lblHuisnummer, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(lblStraat, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(lblPostcode, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblDeelnemers, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtDeelnemers, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPlaats, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtNaam, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtStraat, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtHuisnummer, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPostcode, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnOke, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel)))
                .addContainerGap(15, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNaam)
                    .addComponent(txtNaam, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPlaats, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPlaats))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtStraat, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblStraat))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtHuisnummer, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblHuisnummer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPostcode, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPostcode))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDeelnemers, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDeelnemers))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOke)
                    .addComponent(btnCancel))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtNaamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNaamActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNaamActionPerformed

    private void btnOkeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkeActionPerformed

        clsLocatie.setNaam(txtNaam.getText());
        clsLocatie.setPlaats(txtPlaats.getText());
        clsLocatie.setStraat(txtStraat.getText());
        clsLocatie.setHuisnummer(txtHuisnummer.getText());
        clsLocatie.setPostcode(txtPostcode.getText());
        clsLocatie.setDeelnemers(txtDeelnemers.getText());

        switch (actieCode) {
            case 't':
                clsLocatie.toevoegen();
                break;
            case 'b':
                clsLocatie.bijwerken(this.huisnummer, this.postcode);
                break;
            case 'v':
                clsLocatie.verwijderen();
                break;
        }

        if (clsLocatie.isGevalideerd()) {
            this.setVisible(false);
            new LocatieOverzicht().setVisible(true);
        } else {
            String bericht = "";
            ArrayList berichten = clsLocatie.getBericht();
            for (int i = 0; i < berichten.size(); i++) {
                bericht = bericht + (String) berichten.get(i)+ "\n";
            }
            
           JOptionPane.showMessageDialog(null, bericht);
           clsLocatie.resetBericht();
        }
    }//GEN-LAST:event_btnOkeActionPerformed

    private void txtHuisnummerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtHuisnummerActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtHuisnummerActionPerformed

    private void txtDeelnemersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDeelnemersActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDeelnemersActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LocatieDialoog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LocatieDialoog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LocatieDialoog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LocatieDialoog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LocatieDialoog('t', "", "").setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOke;
    private javax.swing.JLabel lblDeelnemers;
    private javax.swing.JLabel lblHuisnummer;
    private javax.swing.JLabel lblNaam;
    private javax.swing.JLabel lblPlaats;
    private javax.swing.JLabel lblPostcode;
    private javax.swing.JLabel lblStraat;
    private javax.swing.JTextField txtDeelnemers;
    private javax.swing.JTextField txtHuisnummer;
    private javax.swing.JTextField txtNaam;
    private javax.swing.JTextField txtPlaats;
    private javax.swing.JTextField txtPostcode;
    private javax.swing.JTextField txtStraat;
    // End of variables declaration//GEN-END:variables
}
